﻿using Swashbuckle.Application;
using Swashbuckle.Swagger;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Web.Http.Description;

namespace He.Web.App_Start
{

    public class SwaggerHeaderParameter : IOperationFilter
    {
        public void Apply(SwaggerDocsConfig c)
        {
            c.ApiKey(this.Key).Name(this.Name).Description(this.Description).In("header");
            c.OperationFilter(() => this);
        }

        public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            operation.parameters = operation.parameters ?? new List<Parameter>();
            Parameter item = new Parameter {
                name = this.Name,
                description = this.Description,
                @in = "header",
                required = new bool?(this.IsRequired),
                type = "string",
                @default = this.DefaultValue
            };
            operation.parameters.Add(item);
        }

        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
        {
            List<IDictionary<string, IEnumerable<string>>> list2 = new List<IDictionary<string, IEnumerable<string>>>();
            Dictionary<string, IEnumerable<string>> item = new Dictionary<string, IEnumerable<string>>();
            item.Add(this.Key, new string[0]);
            list2.Add(item);
            List<IDictionary<string, IEnumerable<string>>> list = list2;
            swaggerDoc.security = list;
        }

        public string DefaultValue { get; set; }

        public string Description { get; set; }

        public bool IsRequired { get; set; }

        public string Key { get; set; }

        public string Name { get; set; }
    }
}

